package com.shimao.celnet.payment.controller;

import com.shimao.celnet.payment.form.PInvoiceForm;
import com.shimao.celnet.payment.service.BEstateService;
import com.shimao.celnet.payment.service.PInvoiceBlueService;
import com.shimao.celnet.payment.service.PInvoiceRedService;
import com.shimao.celnet.payment.service.SCodeNameService;
import com.shimao.celnet.payment.util.FinalUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 开票界面控制类
 * 1、未开发票，通过guid，打开pc开票界面，取得开票抬头： PInvoicePcForm（pc开票详情）
 * 2、未开发票，通过guid，打开APP电子发票开票界面，取得开票抬头： PInvoiceAppForm（移动开票详情）
 * 3、已开发票，通过guid查看、发票打印、发票作废： PInvoiceForm（发票详情）
 * Created by ensure on 2018/01/17.
 */
@Controller
@RequestMapping("invoice")
public class PInvoiceController {
    protected Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private SCodeNameService sCodeNameService;

    @Autowired
    private BEstateService bEstateService;

    @Autowired
    private PInvoiceBlueService pInvoiceBlueService;

    @Autowired
    private PInvoiceRedService pInvoiceRedService;


    /**
     * 已开发票，通过guid查看预览发票（蓝票，红票）
     *
     * @param invoiceId
     * @param blueRedType：blue:蓝票，red:红票
     * @param model
     * @return
     */
    @RequestMapping(value = "openInv")
    public String invoiceView(@RequestParam String invoiceId, String blueRedType, Model model) {
        logger.info("预览发票--开始");
        String retStr = "invoice/invoiceError";
        //通过开票guid，查询发票记录详情，发票明细
        PInvoiceForm formBean = pInvoiceBlueService.getInvoiceForm(invoiceId, blueRedType);
        formBean.setHeadType("1");//抬头类型
        //发票种类取得：专用发票、普通发票、电子发票、卷式发票
        formBean.setInvoiceKindList(sCodeNameService.getCodeNameListByType(FinalUtil.CODE_NAME_INVOICE_KIND));
        model.addAttribute("formBean", formBean);
        logger.info("预览发票--结束");
        if (FinalUtil.INVOICE_STATUS_1.equals(formBean.getInvoiceStatus())) {
            //外部开票请求，  PC发票界面跳转
            return "invoice/pcInvoice";
        } else if (FinalUtil.INVOICE_SUCCESS.equals(formBean.getInvoiceStatus())) {
            //开票成功，发票预览界面
            return "invoice/invoicePreview";
        }
        return retStr;
    }
    /**
     * 已开发票，通过guid查看预览发票（蓝票，红票）
     *
     * @param invoiceId
     * @param blueRedType：blue:蓝票，red:红票
     * @param model
     * @return
     */
    @RequestMapping(value = "openEInv")
    public String invoiceEView(@RequestParam String invoiceId, String blueRedType, Model model) {
        logger.info("预览发票--开始");
        String retStr = "invoice/invoiceError";
        //通过开票guid，查询发票记录详情，发票明细
        PInvoiceForm formBean = pInvoiceBlueService.getInvoiceForm(invoiceId, blueRedType);
        formBean.setHeadType("1");//抬头类型
        //发票种类取得：专用发票、普通发票、电子发票、卷式发票
        formBean.setInvoiceKindList(sCodeNameService.getCodeNameListByType(FinalUtil.CODE_NAME_INVOICE_KIND));
        model.addAttribute("formBean", formBean);
        logger.info("预览发票--结束");
        if(FinalUtil.INVOICE_STATUS_1.equals(formBean.getInvoiceStatus())) {
            //外部开票请求,APP发票界面跳转
            return "invoice/appInvoice";
        }
        return retStr;
    }

    /**
     * @param
     * @return
     */
    @RequestMapping("/appInvoiceSuccess")
    public String invoiceSuccess() {
        logger.info("发票成功页面--开始");
        return "invoice/appInvoiceSuccess";
    }
}
